import { Histroy } from "./base";

function getHash() {
  return window.location.hash.slice(1);
}

export class HashHistory extends Histroy {
  constructor(router) {
    super(router);
    // 确保当前是hash模式
    ensureHash();
  }
  getCurrentLocation() {
    return getHash();
  }
  setUpHashListener() {
    window.addEventListener("hashchange", () => {
      this.transitionTo(getHash());
    })
  }
}

function ensureHash() {
  if (window.location.hash) {
    return;
  } else {
    window.location.hash = '/'; // 加个斜杠就会自动加一个"#"
  }
}